using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;


namespace FoodWar.Episode1.Components
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class StaticPhysicsBox : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public GameFITClub game;
        public Model model;
        Matrix[] transforms;
        public Vector3 position, scale;
        public Matrix worldMatrix;
        public Body body;

        public StaticPhysicsBox(Game game, float x, float y, float width, float height)
        //public StaticPhysicsBox(Game game, float x1, float y1, float x2, float y2)
            : base(game)
        {
            this.game = (GameFITClub)game;

            position = new Vector3(x, y, 0.0f);
            scale = new Vector3(width, height, 1.0f);

            body = BodyFactory.CreateRectangle(this.game.PhysicsWorld, scale.X, scale.Y, 1f, new Vector2(position.X, position.Y));
            body.IsStatic = true;
            

            worldMatrix =Matrix.CreateScale(scale) * Matrix.CreateTranslation(position);

            game.Components.Add(this);
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            
            //Console.WriteLine(worldMatrix);
            base.Initialize();
        }

        /// <summary>
        /// Loads any component specific content
        /// </summary>
        protected override void LoadContent()
        {
            // TODO: Load any content
            model = game.Content.Load<Model>("box");
            transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        /// <summary>
        /// Allows the game component to draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            // TODO: Add your drawing code here
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.Alpha = 1.0f;
                    //effect.TextureEnabled = false;
                    effect.DiffuseColor = new Vector3(1.0f, 0.0f, 0);
                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;
                    effect.World = transforms[mesh.ParentBone.Index] * worldMatrix;
                    effect.View = game.ViewMatrix;
                    effect.Projection = game.ProjectionMatrix;
                }
                mesh.Draw();
            } 
            //model.Draw(worldMatrix, game.viewMatrix, game.projectionMatrix);
            base.Draw(gameTime);
        }
    }
}
